//引入axios
import axios from 'axios'
import CalcuMD5 from './md5'
import { formatDate } from './utils'

let cancel, promiseArr = {}
const CancelToken = axios.CancelToken
//请求拦截器
axios.interceptors.request.use(config => {
  //发起请求时，取消掉当前正在进行的相同请求
  if (promiseArr[config.url]) {
    promiseArr[config.url]('操作取消')
    promiseArr[config.url] = cancel
  } else {
    promiseArr[config.url] = cancel
  }
  return config
}, error => {
  return Promise.reject(error)
})

//响应拦截器即异常处理
axios.interceptors.response.use(response => {
  return response
}, err => {
  return Promise.resolve(err.response)
})
let url = {
  baseURL: 'http://dev.api.famlink.co' //开发环境
  //baseURL:"http://local.control.famlink.co", //线下
  //baseURL:"http://manage.api.co", //线上
  // baseURL: 'http://alpha.api.famlink.co' //测试环境
//  http://alpha.upload.famlink.co

}
axios.defaults.baseURL = url.baseURL
//设置默认请求头
axios.defaults.headers = {
  'X-Requested-With': 'XMLHttpRequest'
}
axios.defaults.timeout = 10000

// let ua = navigator.userAgent
// let isAndroid = ua.indexOf('android') !== -1

const dataParams = {
  cid: 'paramida-debug-system_djy',
  os: 'IOS-9.3.2',
  network: 1,
  appkey: 'com.raindrop.link',
  appv: '1.1.0-dev',
  sdkv: '1.1.0',
  country: '1.0.0',
  lang: 'zh-CN',
  time: (new Date()).valueOf(),
  local_time: formatDate(new Date().getTime()),
  req_id: Math.random() + (new Date()).valueOf(),
  resolution: '',
  manufacturer: '',
  channel: 'test',
  timezone: -8
}

export default {
  //get请求
  get(url, param) {
    const newParam = Object.assign(dataParams, param)
    const sign = CalcuMD5(url, newParam, 'get')
    return new Promise((resolve, reject) => {
      axios({
        method: 'get',
        url,
        params: Object.assign(dataParams, param, { sign: sign }),
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        let currData = JSON.stringify(res.data)
        currData = currData.replace(new RegExp(/(\?)(.*?webp)/gi), '')
        // console.log('--------->', currData)
        currData = JSON.parse(currData)
        resolve(currData)
      }).catch(err => {
        console.log(err)
      })
    })
  },
  //post请求
  post(url, param) {
    const newParam = Object.assign(dataParams, param)
    const sign = CalcuMD5(url, newParam, 'post')

    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url,
        data: Object.assign(dataParams, param, { sign: sign }),
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        let currData = JSON.stringify(res.data)
        currData = currData.replace(new RegExp(/(\?)(.*?webp)/gi), '')
        // console.log('--------->', currData)
        currData = JSON.parse(currData)
        resolve(currData)
      }).catch(err => {
        console.log(err)
      })
    })
  }
  // upload(url, param) {
  //   return new Promise((resolve, reject) => {
  //     axios({
  //       method: 'post',
  //       url,
  //       data: Object.assign(dataParams, param, { sign: sign }),
  //       cancelToken: new CancelToken(c => {
  //         cancel = c
  //       })
  //     }).then(res => {
  //       resolve(res.data)
  //     }).catch(err => {
  //       console.log(err)
  //     })
  //   })
  // }
}
